/*********************************************************************
Replication Materials for "Explanation Giving Promotes Democratic Satisfaction Regardless of Respondent Partisanship"

Syntax produces the models discussed in Online Appendix E (Tables OE1 and OE2) 
The syntax also produces the output that will be plotted in Figure OE1 (via an R syntax file). 
***********************************************************************/

/*******************
Working Directory and Data
Update working directory to where you have the data
********************/

cd "C:\Users\jar68\OneDrive\Ongoing Work\Explanations and Dem Support\P. Behavior\Revision\R2\RR3\FINAL\Dataverse\"
use "ess_final.dta"

**Dropping non-democracies; Bulgaria had caretaker gov't
drop if country == "Albania" | country == "Russia" | country == "Kosovo" | country == "Ukraine" | country == "Bulgaria"

/*********************************
Weights
*********************************/

gen anweight = pspwght*pweight
label var anweight "Analysis Weight"


/********************
Simple Control: Table OE1
********************/

***Models

eststo clear
local conts " std_courts std_elections std_econ std_life std_socialtrust i.ideol_ext1 std_ladder std_educ c.age i.gender i.minority i.citizen i.east_europe c.enep_comb c.unemp_ch"
foreach var in gov_satis dem_satis {
	eststo: mixed `var' std_explains reason_std i.co_partisan `conts'  [pweight=anweight] || country: 
	eststo: mixed `var' std_explains reason_std i.co_partisan i.coalition_gov `conts'  [pweight=anweight] || country: 
	eststo: mixed `var' c.std_explains##i.co_partisan c.reason_std##i.co_partisan `conts' [pweight=anweight] || country: 
	eststo: mixed `var' c.std_explains##i.co_partisan c.reason_std##i.co_partisan i.coalition_gov `conts'  [pweight=anweight] || country: 
}

***Table

esttab using table_oe1.rtf, label onecell replace se b(3) aic bic nobaselevels ///
		transform(ln*: exp(2*@) 2*exp(2*@)) ///
		eqlabels("" "var(Constant)" "var(Residual)", none) ///
		mtitles("Gov" "Gov" "Gov" "Gov" "Dem" "Dem" "Dem" "Dem") ///
		title("{\b Table OE1}: Replicating Initial Results with Control for Coalition Gov") ///
		addnotes("DV ranges from 0-10. All continuous variables are country-standardized save for age in years. Analyses use survey weights.")

		
/********************
Interactions: Table OE2
********************/

eststo clear
local conts " std_courts std_elections std_econ std_life std_socialtrust i.ideol_ext1 std_ladder std_educ c.age i.gender i.minority i.citizen i.east_europe c.enep_comb c.unemp_ch"
foreach var in gov_satis dem_satis {
	eststo: mixed `var' c.std_explains##i.coalition_gov c.reason_std i.co_partisan `conts' [pweight=anweight] || country: 
		*marginal effect
			margins, dydx(std_explains) by(coalition_gov) saving(`var'_explain_dydx_coalition, replace)
		*predicted values
			margins, at(std_explains=(-2(1)2)) by(coalition_gov) saving(`var'_explain_pred_coalition, replace)
	eststo: mixed `var' c.reason_std##i.coalition_gov c.std_explains i.co_partisan `conts' [pweight=anweight] || country: 
		*marginal effect
			margins, dydx(reason_std) by(coalition_gov) saving(`var'_reason_dydx_coalition, replace)
		*predicted values
			margins, at(reason_std=(-2(1)2)) by(coalition_gov) saving(`var'_reason_pred_coalition, replace)	
	eststo: mixed `var' c.std_explains##i.coalition_gov c.reason_std##i.coalition_gov i.co_partisan `conts' [pweight=anweight] || country: 
		*marginal effect
			margins, dydx(std_explains reason_std) by(coalition_gov) saving(`var'_both_dydx_coalition, replace)
		*predicted values
			margins, at(std_explains=(-2(1)2)) by(coalition_gov) saving(`var'_explain_pred__both_coalition, replace)
			margins, at(reason_std=(-2(1)2)) by(coalition_gov) saving(`var'_reason_pred_both_coalition, replace)	
}


***Full Table
esttab using table_oe2.rtf, label onecell replace se b(3) aic bic nobaselevels ///
		transform(ln*: exp(2*@) 2*exp(2*@)) ///
		eqlabels("" "var(Constant)" "var(Residual)", none) ///
		mtitles("Gov" "Gov" "Gov" "Dem" "Dem" "Dem") ///
		title("{\b Table OE2}: Explanation Giving and Political Support: Coalition Gov Interactions") ///
		addnotes("DV ranges from 0-10. All continuous variables are country-standardized save for age in years. Analyses use survey weights.")
